﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;

namespace Utilities
{
    public class FileHelper
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileLoad">上传控件</param>
        /// <param name="savePath">保存路径</param>
        /// <returns>文件完整路径</returns>
        public static string SaveFile(FileUpload fileLoad, string savePath)
        {
            string filePath = string.Empty;
            if (fileLoad.HasFile)
            {
                string fileName = DateTime.Now.ToFileTimeUtc() + GetExtName(fileLoad.FileName);
                filePath = savePath + fileName;
                savePath = System.Web.HttpContext.Current.Server.MapPath(savePath);
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                fileLoad.SaveAs(savePath + fileName);
            }
            return filePath;
        }

        /// <summary>
        /// 获得文件扩展名
        /// </summary>
        /// <param name="FileName">文件名称</param>
        /// <returns>扩展名</returns>
        public static string GetExtName(string FileName)
        {
            int l = FileName.LastIndexOf(".");
            if (l > -1)
            {
                return FileName.Substring(l, FileName.Length - l);
            }
            else
            {
                return "";
            }
        }
    }
}
